﻿using System;
using System.Xml.Serialization;
using TiPort.TiEntityObjects;
using TI;
using TI_EntityLib.TiEntityObjects;
using TiEntityObject.Common;

namespace TiEntityObject.Contract
{   
    public class Contract : MongoEntity, IDbRootObject 
    {
        [XmlElement("id")]
        public Int64 ID { get; set; }

        public string Key { get { return ID.ToString(); } }

        [XmlElement("regNum")]
        public string RegNum { get; set; }


        [XmlElement("regNum807")]
        public string regNum807  { get; set; }

        [XmlElement("number")]
        public string Number { get; set; }

        [XmlElement("publishDate")]
        public DateTime  PublishDate { get; set; }
      


        [XmlElement("signDate")]
        public DateTime SignDate { get; set; }
        public long iSignDate { get { return Helper.Time.GetDateInMS(SignDate); } }

        [XmlElement("versionNumber")]
        public Int64  versionNumber { get; set; }


        [XmlElement("protocolDate")]
        public DateTime ProtocolDate { get; set; }
        public long iProtocolDate { get { return Helper.Time.GetDateInMS(ProtocolDate); } }

        [XmlElement("documentBase")]
        public string documentBase { get; set; }

        [XmlElement("price")]
        public string Price { get; set; }
        public decimal iPrice {
            get{
                decimal v= 0;
                decimal.TryParse (Price,out v);
                return v;
                }
        }

        [XmlElement("href")]
        public string Href { get; set; }

        [XmlElement("foundation")]
        [LinkObjectAttribute(ChildFieldName = "ID", IsPartialObject = true, ParentFieldName = "ID")]
        public ContractFoundation Foundation { get; set; }

        [XmlElement("finances")]
        [LinkObjectAttribute(ChildFieldName = "ID", IsPartialObject = true, ParentFieldName = "ID")]
        public ContractFinances Finances { get; set; }

        [XmlElement("execution")]
        [LinkObjectAttribute(ChildFieldName = "ID", IsPartialObject = true, ParentFieldName = "ID")]
        public ContractExecution Execution { get; set; }
        
        [XmlElement("customer")]
        [LinkObjectAttribute(ChildFieldName = "RegNum", IsDictionaryValue = false, ParentFieldName = "CustomerID")]
        public Customer Customer { get; set; }

        [XmlElement("currency")]
        [LinkObjectAttribute(ChildFieldName = "code", IsPartialObject =false,  IsDictionaryValue = true, ParentFieldName = "CurrencyID")]
        public Currency Currency { get; set; }

        [XmlElement("products")]    
        [LinkObjectAttribute(ChildFieldName = "ContractID", IsCollection = true, IsPartialObject = true, ParentFieldName = "ID")]
        public ContractProducts Products { get; set; }


        [XmlElement("suppliers")]
        [LinkObjectAttribute(ChildFieldName = "ContractID", IsCollection = true, IsPartialObject = true, ParentFieldName = "ID")]
        public ContractSuppliers Suppliers { get; set; }

        [XmlElement("documentMetas")]
        [LinkObjectAttribute(ChildFieldName = "NotificationID", IsCollection = true, IsPartialObject = true, ParentFieldName = "ID")]
        public ContractDocumentMetas DocumentMetas { get; set; }

        [XmlElement("other")]
        [LinkObjectAttribute(ChildFieldName = "ContractID",  IsPartialObject = true, ParentFieldName = "ID")]
        public Other Other { get; set; }

        [XmlElement("printForm")]
        [LinkObjectAttribute(ChildFieldName = "ContractID", IsPartialObject = true, ParentFieldName = "ID")]
        public ContractPrintForm PrintForm { get; set; }

        public DateTime LastUpdateDate { get { return DateTime.Now; } set { ;} }
        public long iLastUpdateDate { get { return Helper.Time.GetDateInMS(LastUpdateDate); } }

        public string FileName {get;set;}

        public override string GetAsJson()
        {
            return Helper.Json<Contract>.NewtonSerializeObject(this);
        }

        public override string GetCollectionName()
        {
            return "Contract";
        }
     
        public long iFileDate { get; set; }

        public override string GetInsertScript()
        {
            string v = GetAsJson();
            return "mergeContract(" + v + ")";
        }
     
        public TiEntityClass Class
        {
            get { return TiEntityClass.Pharm ; }
        }

        public string ObjectType
        {
            get;
            set;
        }


        public long iTransportDay
        {
            get;
            set;
        }
    }
}
